package com.app.ytong.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatImageView;

import com.app.ytong.R;
import com.qmuiteam.qmui.skin.QMUISkinHelper;
import com.qmuiteam.qmui.skin.QMUISkinValueBuilder;
import com.qmuiteam.qmui.util.QMUIResHelper;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogMenuItemView;

public class CustomChooseItemView   extends QMUIDialogMenuItemView {

    private Context mContext;
    private TextView mTextView;
    private AppCompatImageView mCheckedView;

    @SuppressLint("CustomViewStyleable")
    public CustomChooseItemView(Context context) {
        super(context);
        mContext = context;
        mCheckedView = new AppCompatImageView(mContext);
        mCheckedView.setId(View.generateViewId());

        TypedArray a = context.obtainStyledAttributes(null, R.styleable.QMUIDialogMenuMarkDef,
                R.attr.qmui_dialog_menu_item_style, 0);
        int markMarginHor = 0;
        int count = a.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attr = a.getIndex(i);
            if (attr == R.styleable.QMUIDialogMenuMarkDef_qmui_dialog_menu_item_check_mark_margin_hor) {
                markMarginHor = a.getDimensionPixelSize(attr, 0);
            } else if (attr == R.styleable.QMUIDialogMenuMarkDef_qmui_dialog_menu_item_mark_drawable) {
                mCheckedView.setImageDrawable(QMUIResHelper.getAttrDrawable(context, a, attr));
            }
        }
        a.recycle();
        QMUISkinValueBuilder builder = QMUISkinValueBuilder.acquire();
        builder.src(R.attr.qmui_skin_support_dialog_mark_drawable);
        QMUISkinHelper.setSkinValue(mCheckedView, builder);
        QMUISkinValueBuilder.release(builder);

        LayoutParams checkLp = new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        checkLp.rightToRight = LayoutParams.PARENT_ID;
        checkLp.topToTop = LayoutParams.PARENT_ID;
        checkLp.bottomToBottom = LayoutParams.PARENT_ID;
        addView(mCheckedView, checkLp);


        mTextView = createItemTextView(mContext);
        mTextView.setSingleLine(false);
        LayoutParams tvLp = new LayoutParams(0, 0);
        tvLp.leftToLeft = LayoutParams.PARENT_ID;
        tvLp.topToTop = LayoutParams.PARENT_ID;
        tvLp.bottomToBottom = LayoutParams.PARENT_ID;
        tvLp.rightToLeft = mCheckedView.getId();
        tvLp.rightMargin = markMarginHor;
        addView(mTextView, tvLp);
        mCheckedView.setVisibility(INVISIBLE);
    }

    public CustomChooseItemView(Context context, CharSequence text) {
        this(context);
        setText(text);
    }

    public void setText(CharSequence text) {
        mTextView.setText(text);
    }

    @Override
    protected void notifyCheckChange(boolean isChecked) {
        mCheckedView.setVisibility(isChecked ? VISIBLE : INVISIBLE);
    }
}
